package edu.hawaii.halealohacli;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.wattdepot.client.WattDepotClient;

/**
 * JUnit test used to check and test the monitor-power class.
 * 
 * @author John MacMillan
 * @author Mitchell Kupfer
 * @author Bryson Hori
 * @author Chris Fang
 */
public class TestMonitorPower {
  // URL to the WattDepot server
  String url = "http://server.wattdepot.org:8190/wattdepot/";
  // Create a WattDepotClient with URL
  WattDepotClient client = new WattDepotClient(url);
  String towerName = "Mokihana";
  String towerName2 = "bar";
  int interval = 1;
  MonitorPower defaultMonitorPower = new MonitorPower(towerName, client);
  MonitorPower customMonitorPower = new MonitorPower(towerName, client, interval);

  /**
   * Checks to see if the values entered in the constructor for monitor-power processes right.
   */
  @Test
  public void testValues() {
    assertEquals("Check if right values are set", towerName, defaultMonitorPower.getTower());
    assertEquals("Check if right values are set", towerName, customMonitorPower.getTower());
  }

  /**
   * Test that a printer thread can be created and stopped.
   */
  @Test
  public void testPrinter() {
    boolean succeeded = false;
    try {
      PrinterThread printer = new PrinterThread(towerName, client, interval);
      printer.start();
      Thread.sleep(2000);
      printer.exit();
      printer.interrupt();
      succeeded = true;
    }
    catch (Exception e) {
      succeeded = false;
    }
    assertTrue("Thread couldn't be created and stopped", succeeded);
  }

  /**
   * Test the printer thread's print method of monitor-power to see if the right outcome occurs.
   */
  @Test
  public void testPrinterMethod() {
    PrinterThread printer = new PrinterThread(towerName, client, interval);
    try {
      assertTrue("Test run with good values", printer.print());
    }
    catch (Exception e) {
      System.out.println("Error occured during testing (testPrinterMethod)");
    }
    PrinterThread badPrinter = new PrinterThread(towerName2, client, interval);
    try {
      assertFalse("Test run with bad values", badPrinter.print());
    }
    catch (Exception e) {
      System.out.println("Error occured during testing (testPrinterMethod)");
    }
  }

  /**
   * Test the testable parts of the run method.
   */
  @Test
  public void testRun() {
    defaultMonitorPower.setTesting(true);
    try {
      assertTrue("Run completed", defaultMonitorPower.run());
    }
    catch (Exception e) {
      System.out.println("Error occured when running monitor-power.");
    }
  }
}
